//
//  XmlParserMarkedTypeList.m
//  FleaFinder
//
//  Created by Hieu Le on 4/19/13.
//  Copyright (c) 2013 Kiwiweb. All rights reserved.
//

#import "XmlParserMarkedTypeList.h"

@implementation XmlParserMarkedTypeList

-(id)init
{
    self = [super init];
    
    if(self != nil){
        tagMarkedTypes = [NSString stringWithFormat:@"markedtypes"];
        tagMarkedType = [NSString stringWithFormat:@"markedtype"];
        //tagBelongTo = [NSString stringWithFormat:@"categories"];
        tagId = [NSString stringWithFormat:@"id"];
        tagName = [NSString stringWithFormat:@"name"];
        
        parsedObjects = [NSMutableArray arrayWithCapacity:6];
        isInsideTagMarkedType = NO;
        
        count = 1;
    }
    
    return self;
}

-(NSMutableArray*) getParsedObjects
{
    return parsedObjects;
}

-(void) parseXML:(NSString *)sourceUrl
{     
    NSURL *url = [[NSURL alloc] initWithString: sourceUrl];
    //NSData* data =  [NSData dataWithContentsOfURL:url];
    
    NSError *error;
    NSString * dataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    
    NSData *data = [dataString dataUsingEncoding:NSUnicodeStringEncoding];
    
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
    [xmlParser setDelegate:self];
    [xmlParser parse];}

#pragma implementation of NSXMLParserDelegate

-(void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    //NSLog(@"Error = %@", parseError);
}

-(void) parserDidStartDocument:(NSXMLParser *)parser
{
    //NSLog(@"Start the document");
    if(parsedObjects){
        [parsedObjects removeAllObjects];
    }
}

-(void) parserDidEndDocument:(NSXMLParser *)parser
{
    //NSLog(@"End the document");
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    //static NSString *currentMarkedType = nil;
    
    //NSLog(@"Start element name: %@", elementName);
    //NSLog(@"value of attribute NAME: %@", [attributeDict objectForKey:@"name"]);
    
    if ([tagMarkedType compare:elementName] == NSOrderedSame) {
        
        currentMarkedType = [[MarketType alloc] init];
        currentMarkedType.pos = count++;
        
        isInsideTagMarkedType = YES;
        
    } else if([tagId compare:elementName] == NSOrderedSame){
        currentTag = MARKET_TYPE_TAG_ID;
    } else if([tagName compare:elementName] == NSOrderedSame){
        currentTag = MARKET_TYPE_TAG_NAME;
    }
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    //NSLog(@"End element name: %@", elementName);
    /*if(currentMarkedType != nil){
        [parsedObjects addObject:currentMarkedType];
        currentMarkedType = nil;
    }*/
    
    if ([tagMarkedType compare:elementName] == NSOrderedSame) {
        [parsedObjects addObject:currentMarkedType];
        currentMarkedType = nil;
        isInsideTagMarkedType = NO;
    }
    
    
    currentTag = -1;
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    //NSLog(@"Character: %@", string);
    if([string rangeOfString:@"\n"].location != NSNotFound){
        return;
    }
    if(isInsideTagMarkedType){
        if(currentTag == MARKET_TYPE_TAG_ID){
            currentMarkedType.markedId = [string intValue];
        }else if(currentTag == MARKET_TYPE_TAG_NAME){
            if(currentMarkedType.name != nil){
                currentMarkedType.name = [NSString stringWithFormat:@"%@%@", currentMarkedType.name, string];
            } else {
                currentMarkedType.name = string;
            }
        }
    }
}

@end

